package top.continew.admin.controller.pdnew;

import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.base.TransResult;
import cn.shengchao.examstar.rpc.distribute.req.ProductThirdDistributeProxyIdReq;
import cn.shengchao.examstar.rpc.distribute.req.ThirdProductDistributeUserQueryPage;
import cn.shengchao.examstar.rpc.distribute.resp.ThirdProductDistributeForUserRespEntity;
import cn.shengchao.examstar.rpc.distribute.resp.ThirdProductDistributeProxyInfoRespEntity;
import cn.shengchao.examstar.rpc.product.api.IContentProductTypeRpc;
import cn.shengchao.examstar.rpc.product.base.ProductTypeTree;
import cn.shengchao.examstar.rpc.product.base.SimpleProduct;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.req.pdnew.ProductThirdDistributeProxySaveReq;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * @program: examstar-manger-api
 * @description: 内容产品分类
 * @author: Xiong weiqiang
 * @create: 2025-06-10 16:44
 */
@Tag(name = "内容产品分类API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/cProductType")
public class ContentProductTypeController {
    @DubboReference
    private IContentProductTypeRpc iContentProductTypeRpc;
    /**
     * 内容产品分类树
     * @return
     */
    @Operation(summary = "内容产品分类树", description = "内容产品分类树")
    @GetMapping("/listType")
    R<List<ProductTypeTree>> listType() {
        return R.ok(iContentProductTypeRpc.getContentProductTypes());
    }
    /**
     * 内容产品分类列表
     * @return
     */
    @Operation(summary = "内容产品分类列表", description = "内容产品分类列表")
    @GetMapping("/listProductTypes")
    R<List<SimpleProduct>> listProductTypes() {
        return R.ok(iContentProductTypeRpc.listProductDetails());
    }

    @Operation(summary = "列出内容供应商的会员分销代理列表", description = "列出内容供应商的会员分销代理列表")
    @GetMapping("/listThirdMemberProxy")
    PageResult<ThirdProductDistributeForUserRespEntity> pageQueryByUserId(ThirdProductDistributeUserQueryPage pageQuery) {
        return iContentProductTypeRpc.getThirdProductDistributeListForUser(pageQuery);
    }
    @Operation(summary = "列出内容供应商的已代理信息列表", description = "列出内容供应商的已代理信息列表")
    @GetMapping("/listThirdMemberProxyInfo/{proxyProductId}")
    R<List<ThirdProductDistributeProxyInfoRespEntity>> selectListByProductId(@PathVariable Long proxyProductId) {
        return R.ok(iContentProductTypeRpc.selectListByProductId(proxyProductId));
    }
    @Operation(summary = "新增会员分销代理设置", description = "新增会员分销代理设置")
    @PostMapping("/addThirdMemberProxy")
    public R<String> addThirdMemberProxy(@Validated @RequestBody ProductThirdDistributeProxySaveReq req) {
        TransResult<String> transResult=iContentProductTypeRpc.addThirdProductDistributeProxyForUser(req.toProductThirdDistributeProxyReq());
        if (transResult.getSuccess()){
            return R.ok(transResult.getMessage());
        }else {
            return R.fail(R.fail().getCode(), transResult.getMessage());
        }
    }
    @Operation(summary = "删除会员分销代理设置", description = "删除会员分销代理设置")
    @PostMapping("/deleteThirdMemberProxy/{proxyProductId}/{userId}")
    public R<String> deleteThirdMemberProxy(@PathVariable Long proxyProductId,@PathVariable Long userId) {
        ProductThirdDistributeProxyIdReq req=new ProductThirdDistributeProxyIdReq();
        req.setProductId(proxyProductId);
        req.setUserId(userId);
        TransResult<String> transResult=iContentProductTypeRpc.deleteThirdProductDistributeProxyForUser(req);
        if (transResult.getSuccess()){
            return R.ok(transResult.getMessage());
        }else {
            return R.fail(R.fail().getCode(), transResult.getMessage());
        }
    }
    @Operation(summary = "会员分销代理设置编辑加载", description = "会员分销代理设置编辑加载")
    @GetMapping("/loadThirdMemberProxy/{userId}/{proxyProductId}")
    public R<String> loadThirdMemberProxy(@PathVariable  Long userId,@PathVariable Long proxyProductId) {
        ThirdProductDistributeForUserRespEntity respEntity=iContentProductTypeRpc.loadThirdMemberProxy(userId,proxyProductId);
        if (respEntity!=null&&respEntity.getProductId()!=null){
            return R.ok(respEntity);
        }
        return R.fail();
    }
    @Operation(summary = "列出会员分销代理预加载列表", description = "列出会员分销代理预加载列表")
    @GetMapping("/listThirdMemberProduct/{userId}")
    public R<List<SimpleProduct>> listThirdMemberProductExcludeUser(@PathVariable Long userId) {
        List<SimpleProduct> simpleProducts=iContentProductTypeRpc.listThirdMemberProductExcludeUser(userId);
        return R.ok(simpleProducts);
    }
}